﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Text;
using System.Text.RegularExpressions;
using System.Net;
using System.Web;
using System.Web.Security;

namespace Tickz.Commonz
{
    public class CommonFunction
    {
        #region 加密密码
        /// <summary>
        /// 加密密码
        /// </summary>
        /// <param name="pass"></param>
        /// <returns></returns>
        //public static string DESDecrypt(string pass)
        //{
        //    try
        //    {
        //        DESCryptoServiceProvider provider = new DESCryptoServiceProvider();
        //        byte[] buffer = Convert.FromBase64String(pass);
        //        MemoryStream stream = new MemoryStream();
        //        CryptoStream stream2 = new CryptoStream(stream, provider.CreateDecryptor(Param.Key_64, Param.Iv_64), CryptoStreamMode.Write);
        //        stream2.Write(buffer, 0, buffer.Length);
        //        stream2.FlushFinalBlock();
        //        string str = Encoding.UTF8.GetString(stream.ToArray());
        //        stream.Close();
        //        return str;
        //    }
        //    catch
        //    {
        //        return pass;
        //    }
        //} 
        #endregion

        #region 解密pass (des64)
        /// <summary>
        /// 解密pass (des64)
        /// </summary>
        /// <param name="pass"></param>
        /// <returns></returns>
        //public static string DESEncrypt(string pass)
        //{
        //    try
        //    {
        //        DESCryptoServiceProvider provider = new DESCryptoServiceProvider();
        //        byte[] bytes = Encoding.UTF8.GetBytes(pass);
        //        MemoryStream stream = new MemoryStream();
        //        CryptoStream stream2 = new CryptoStream(stream, provider.CreateEncryptor(Param.Key_64, Param.Iv_64), CryptoStreamMode.Write);
        //        stream2.Write(bytes, 0, bytes.Length);
        //        stream2.FlushFinalBlock();
        //        byte[] inArray = stream.ToArray();
        //        stream.Close();
        //        return Convert.ToBase64String(inArray);
        //    }
        //    catch
        //    {
        //        return pass;
        //    }
        //} 
        #endregion

        #region 根据 当前页 页数 每页数据量 返回分页条
        /// <summary>
        /// 根据 当前页 页数 每页数据量 返回分页条
        /// </summary>
        /// <param name="pageIndex"></param>
        /// <param name="pageCount"></param>
        /// <param name="length"></param>
        /// <returns></returns>
        public static string GetPageNumStr(int pageIndex, int pageCount, int length)
        {
            if (pageIndex < 1)
            {
                pageIndex = 1;
            }
            if (pageIndex > pageCount)
            {
                pageIndex = pageCount;
            }
            if (pageCount > 0)
            {
                int num3;
                StringBuilder builder = new StringBuilder();
                int num = length / 2;
                int num2 = length - num;
                if (pageIndex <= num)
                {
                    for (num3 = 1; num3 < pageIndex; num3++)
                    {
                        builder.Append(num3);
                        builder.Append("|");
                    }
                    builder.Append(pageIndex);
                    builder.Append("|");
                    for (num3 = pageIndex + 1; (num3 <= length) && (num3 <= pageCount); num3++)
                    {
                        builder.Append(num3);
                        builder.Append("|");
                    }
                }
                if ((pageIndex > num) && ((pageIndex + num2) > pageCount))
                {
                    int num4 = (pageCount - length) + 1;
                    if (num4 < 1)
                    {
                        num4 = 1;
                    }
                    for (num3 = num4; num3 < pageIndex; num3++)
                    {
                        builder.Append(num3);
                        builder.Append("|");
                    }
                    builder.Append(pageIndex);
                    builder.Append("|");
                    for (num3 = 1; (num3 < num2) && ((pageIndex + num3) <= pageCount); num3++)
                    {
                        builder.Append((int)(pageIndex + num3));
                        builder.Append("|");
                    }
                }
                if ((pageIndex > num) && ((pageIndex + num2) <= pageCount))
                {
                    for (num3 = num; num3 > 0; num3--)
                    {
                        builder.Append((int)(pageIndex - num3));
                        builder.Append("|");
                    }
                    builder.Append(pageIndex);
                    builder.Append("|");
                    for (num3 = 1; (num3 < num2) && ((pageIndex + num3) <= pageCount); num3++)
                    {
                        builder.Append((int)(pageIndex + num3));
                        builder.Append("|");
                    }
                }
                string str = builder.ToString();
                if (str.EndsWith("|"))
                {
                    return str.Substring(0, str.Length - 1);
                }
            }
            return string.Empty;
        }
        #endregion

        #region 分割字符
        /// <summary>
        /// 分割字符
        /// </summary>
        /// <param name="strOriginal"></param>
        /// <param name="strSymbol"></param>
        /// <returns></returns>
        public static string[] GetSplit(string strOriginal, string strSymbol)
        {
            return strOriginal.Split(new char[] { Convert.ToChar(strSymbol) });
        }
        #endregion

        #region 字符长度格式化
        /// <summary>
        /// 字符长度格式化
        /// </summary>
        /// <param name="Str"></param>
        /// <returns></returns>
        public static string GetStrByByte(string Str)
        {
            double num3;
            double num = 1024.0;
            double length = Encoding.Default.GetBytes(Str).Length;
            if (length < 1024.0)
            {
                return (length.ToString() + " Byte");
            }
            if ((length / num) < 1024.0)
            {
                num3 = length / num;
                return (num3.ToString("0.00") + " KB");
            }
            num3 = (length / num) / num;
            return (num3.ToString("0.00") + " MB");
        }
        #endregion

        #region 被选择就返回对号
        /// <summary>
        /// 被选择就返回对号
        /// </summary>
        /// <param name="str"></param>
        /// <returns></returns>
        public static string GetStyleTrue(string str)
        {
            if ((str == "True") || (str == "1"))
            {
                return "√";
            }
            return "\x00d7";
        }
        #endregion

        #region md5加密成字符串
        /// <summary>
        /// md5加密成字符串
        /// </summary>
        /// <param name="pass"></param>
        /// <returns></returns>
        public static string MD5Encrypt(string pass)
        {
            return FormsAuthentication.HashPasswordForStoringInConfigFile(pass, "MD5").ToLower();
        }
        #endregion

        #region 判断该cookesName的值是否等于CookiesValue
        /// <summary>
        /// 判断该cookesName的值是否等于CookiesValue
        /// </summary>
        /// <param name="CookiesName"></param>
        /// <param name="CookiesValue"></param>
        /// <returns></returns>
        public static bool ReadTempCookies(string CookiesName, string CookiesValue)
        {
            return ((HttpContext.Current.Request.Cookies[CookiesName] != null) && (HttpContext.Current.Request.Cookies[CookiesName].Value == CookiesValue));
        }
        #endregion

        #region 保存该cookie 2小时过期
        /// <summary>
        /// 保存该cookie 2小时过期
        /// </summary>
        /// <param name="CookiesName"></param>
        /// <param name="CookiesValue"></param>
        public static void SaveTempCookies(string CookiesName, string CookiesValue)
        {
            HttpCookie cookie = new HttpCookie(CookiesName, CookiesValue);
            cookie.Expires = DateTime.Now.AddHours(2.0);
            HttpContext.Current.Response.Cookies.Add(cookie);
        }
        #endregion       
        
    }
}
